<template>
  <div class="dev">
    <!-- 搜索框 -->
    <div class="search">
      <el-input placeholder="请输入工单名称" clearable prefix-icon="el-icon-search" v-model="otaTaskName">
      </el-input>
      <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      <el-button type="success" icon="el-icon-plus" @click="DialogVisible = true">添加</el-button>
    </div>
    <!-- 表格数据 -->
    <div class="table">
      <el-table :data="tableData" ref="isExpand" v-loading="loading" border height="calc(100%-10rem)"
        highlight-current-row stripe style="width: 100%" @selection-change="handleSelectionChange">
        >
        <template slot="empty">
          <el-empty description="暂无数据"></el-empty>
        </template>
        <!-- <el-table-column type="selection" width="55"> </el-table-column> -->
        <el-table-column align="center" prop="otaTaskName" label="工单名称">
        </el-table-column>
        <el-table-column align="center" prop="versionNum" label="版本号">
        </el-table-column>
        <el-table-column align="center" prop="description" label="备注">
        </el-table-column>
        <el-table-column align="center" prop="endTime" label="截止时间">
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="edit(scope.row)">备注</el-button>
            <el-button type="text" size="small" @click="upDateRecord(scope.row)">升级记录</el-button>
            <el-button type="text" size="small" @click="delFn(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination v-show="tableData.length !== 0" background @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :current-page="pageNum" :page-sizes="[5, 10, 15, 20, 25, 30]"
        :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <!-- 修改备注 -->
    <div class="dialog">
      <el-dialog title="修改备注" :visible.sync="editDialogVisible" width="600px">
        <el-input type="textarea" :rows="3" placeholder="请输入内容" v-model="textarea">
        </el-input>

        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editFn">确 认</el-button>
        </span>
      </el-dialog>
    </div>
    <!-- 添加-->
    <div class="add">
      <el-dialog title="新增" :visible.sync="DialogVisible" width="600px">
        <el-form :model="addForm" ref="addForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="版本号" prop="otaVersionId">
            <el-cascader :options="options" v-model="addForm.otaVersionId" :show-all-levels="false" clearable
              :props="{ expandTrigger: 'hover' }"></el-cascader>
          </el-form-item>
          <el-form-item label="工单名称" prop="otaTaskName">
            <el-input v-model="addForm.otaTaskName" clearable placeholder="请输入工单名称"></el-input>
          </el-form-item>
          <el-form-item label="截止日期" prop="endTime">
            <el-date-picker v-model="addForm.endTime" type="date" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd"
              placeholder="选择日期" :picker-options="pickerOptions">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="备注" prop="description">
            <el-input v-model="addForm.description" clearable :rows="3" type="textarea" placeholder="请输入内容"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="DialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addFn">确 认</el-button>
        </span>
      </el-dialog>
    </div>

    <!-- 升级记录 -->
    <div class="table">
      <el-dialog title="升级记录" :visible.sync="dialogUp" width="50%">
        <div class="search">
          <el-input placeholder="请输入表号" clearable prefix-icon="el-icon-search" v-model="meterNum">
          </el-input>
          <el-button type="primary" icon="el-icon-search" @click="searchRec">搜索</el-button>
        </div>
        <el-table v-loading="loading1" ref="table" :data="recordData" border height="520" :stripe="true"
          style="width: 100%">
          <template slot="empty">
            <el-empty description="暂无记录"></el-empty>
          </template>
          <el-table-column prop="meterNumber" label="表号"> </el-table-column>
          <el-table-column prop="meterCode" label="编号"> </el-table-column>
          <el-table-column prop="otaTaskProcess" label="状态">
            <template slot-scope="scope">
              <el-tag type="warning" v-if="scope.row.otaTaskProcess == 0">待升级</el-tag>
              <el-tag v-if="scope.row.otaTaskProcess == 1">升级中...</el-tag>
              <el-tag type="success" v-if="scope.row.otaTaskProcess == 2">升级成功</el-tag>
              <el-tag type="danger" v-if="scope.row.otaTaskProcess == 3">升级失败</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间">
          </el-table-column>
          <el-table-column prop="endTime" label="结束时间"> </el-table-column>
          <el-table-column prop="expireTime" label="过期时间">
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination v-show="recordData.length !== 0" background @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1" :current-page="pageNum1" :page-sizes="[5, 10, 15, 20, 25, 30]"
          :page-size="pageSize1" layout="total, sizes, prev, pager, next, jumper" :total="total1">
        </el-pagination>
      </el-dialog>
    </div>
  </div>
</template>
  
<script>
import {
  getTaskApi,
  addTaskApi,
  getHardwareApi,
  getVersionApi,
  delTaskApi,
  editTaskApi,
  upDateRecordApi,
} from "../../../../api/system/task";
export default {
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          let curDate = new Date().getTime();
          let one = 30 * 24 * 3600 * 1000;
          let oneMonths = curDate + one;
          return time.getTime() < Date.now() || time.getTime() > oneMonths;
        },
      },
      otaTaskName: "",
      pageNum: 1,
      pageSize: 10,
      total: null,
      total1: null,
      recordData: [],
      dialogUp: false,
      loading1: false,
      editDialogVisible: false,
      loading: false,
      textarea: "",
      tableData: [],
      DialogVisible: false,
      pageNum: 1,
      pageSize: 10,
      total: null,
      options: [],
      otaTaskId: "",
      meterNum: "",
      recordId: "",
      addForm: {
        description: "",
        endTime: "",
        otaTaskName: "",
        otaVersionId: "",
      },
    };
  },
  created() {
    this.pageSize = Number(sessionStorage.getItem("pageSize")) || 10;
    this.pageNum = Number(sessionStorage.getItem("pageNum")) || 1;
    this.pageSize1 = Number(sessionStorage.getItem("pageSize1")) || 10;
    this.pageNum1 = Number(sessionStorage.getItem("pageNum1")) || 1;
    this.getTask(this.pageNum, this.pageSize);
    this.getOptitons();
  },
  destroyed() {
    sessionStorage.removeItem("pageNum");
    sessionStorage.removeItem("pageSize");
    sessionStorage.removeItem("pageNum1");
    sessionStorage.removeItem("pageSize1");
  },
  methods: {
    // 打开修改备注
    edit(row) {
      this.otaTaskId = row.otaTaskId;
      this.editDialogVisible = true;
      this.textarea = row.description;
    },
    // 确认修改
    editFn() {
      let arr = {
        description: this.textarea,
      };
      editTaskApi(this.otaTaskId, arr)
        .then((res) => {
          this.editDialogVisible = false;
          this.$message.success("操作成功");
          this.search();
        })
        .catch((err) => {
          this.$message.error("修改失败");
        });
    },
    // 添加
    addFn() {
      let arr = {
        description: this.addForm.description,
        endTime: `${this.addForm.endTime} 23:59:59`,
        otaTaskName: this.addForm.otaTaskName,
        otaVersionId: this.addForm.otaVersionId[1],
      };
      addTaskApi(arr)
        .then((Res) => {
          this.DialogVisible = false;
          this.$message.success("添加成功");
          this.search();
        })
        .catch((err) => {
          this.$message.error("添加失败");
        });
    },
    // 获取options
    getOptitons() {
      this.options = [];
      getHardwareApi().then((res) => {
        res.data.forEach((item) => {
          let arr = {
            value: item.hardwareId,
            label: item.hardwareName,
            children: [],
          };
          getVersionApi(item.hardwareId).then((Res) => {
            Res.data.forEach((it) => {
              let children = {
                value: it.versionId,
                label: it.versionNum,
              };
              arr.children.push(children);
            });
          });
          this.options.push(arr);
        });
      });
    },
    // 获取工单列表
    getTask(pageNum, pageSize, otaTaskName) {
      this.loading = true;
      getTaskApi(pageNum, pageSize, otaTaskName).then((res) => {
        this.total = res.data.total;
        if (res.data.rows.length == 0) {
          this.pageNum = this.pageNum - 1;
          if (this.pageNum > 0) {
            this.search();
          } else {
            this.loading = false;
            this.tableData = res.data.rows;
          }
        } else {
          this.loading = false;
          this.tableData = res.data.rows;
        }
      });
    },
    // 删除
    delFn(e) {
      this.$confirm(
        "确认删除此工单？删除后此工单的升级记录将会被清空!",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          delTaskApi(e.otaTaskId)
            .then((res) => {
              this.$message.success("删除成功");
              this.search();
            })
            .catch((err) => {
              this.$message.error("删除失败！");
            });
        })
        .catch((err) => {
          this.$message.success("已取消删除");
        });
    },
    // 查询
    search() {
      this.pageNum = 1;
      this.getTask(this.pageNum, this.pageSize, this.otaTaskName);
      this.$refs.isExpand.bodyWrapper.scrollTop = 0;
    },
    // 查询升级记录
    searchRec() {
      this.pageNum1 = 1;
      this.getRecord(
        this.recordId,
        this.pageNum1,
        this.pageSize1,
        this.meterNum
      );
      this.$refs.table.bodyWrapper.scrollTop = 0;
    },
    // 获取升级记录
    getRecord(id, pageNum, pagesize, meterNum) {
      this.loading1 = true;
      upDateRecordApi(id, pageNum, pagesize, meterNum).then((res) => {
        this.recordData = res.data.rows;
        this.total1 = res.data.total;
        this.loading1 = false;
      });
    },
    // 打开升级记录
    upDateRecord(e) {
      this.recordId = e.otaTaskId;
      this.dialogUp = true;
      this.getRecord(e.otaTaskId, this.pageNum1, this.pageSize1, this.meterNum);
    },
    // 分页
    handleSizeChange1(val) {
      this.pageNum1 = 1;
      this.getRecord(this.recordId, this.pageNum1, val, this.taskName);
      sessionStorage.setItem("pageSize1", val);
      this.pageSize1 = val;
      this.$refs.table.bodyWrapper.scrollTop = 0;
    },
    handleCurrentChange1(val) {
      this.getRecord(this.recordId, val, this.pageSize1, this.taskName);
      sessionStorage.setItem("pageNum1", val);
      this.pageNum1 = val;
      this.$refs.table.bodyWrapper.scrollTop = 0;
    },
    // 分页
    handleSizeChange(val) {
      this.pageNum = 1;
      this.getTask(this.pageNum, val);
      sessionStorage.setItem("pageSize", val);
      this.pageSize = val;
      this.$refs.isExpand.bodyWrapper.scrollTop = 0;
    },
    handleCurrentChange(val) {
      this.getTask(val, this.pageSize);
      sessionStorage.setItem("pageNum", val);
      this.pageNum = val;
      this.$refs.isExpand.bodyWrapper.scrollTop = 0;
    },
    // 批量操作
    handleSelectionChange(e) {
      let arr = [];
      e.forEach((item) => {
        arr.push(item.userId);
      });
      this.ids = arr;
    },
  },
};
</script>
  
  
<style lang="scss" scoped>
.dev {
  height: calc(100vh - 100px);
  padding: 0 15px;

  .search {
    margin-bottom: 15px;

    .el-input {
      width: 200px;
    }

    .el-button {
      margin-left: 15px;
    }
  }

  .table {
    height: 100%;

    .el-table {
      height: 70%;
    }

    .el-pagination {
      text-align: center;
      margin-top: 20px;
    }
  }
}
</style>